﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace HWR_Ants.Classes
{
    public class WorldBuilder
    {
        private readonly Dictionary<string, City> ro_Cities = new Dictionary<string, City>();
        private readonly List<Road> ro_Roads = new List<Road>();

        internal IEnumerable<City> Cities
        {
            get { return ro_Cities.Values; }
        }

        internal IEnumerable<Road> Roads
        {
            get { return ro_Roads; }
        }

        public City AddCity(string name)
        {
            City city = new City(name);
            ro_Cities.Add(name, city);
            return city;
        }

        public void AddCity(City city)
        {
            ro_Cities.Add(city.Name, city);
        }

        public void AddCities(IEnumerable<string> names)
        {
            foreach (string name in names)
            {
                AddCity(name);
            }
        }

        public void AddCities(IEnumerable<City> cities)
        {
            foreach (City city in cities)
            {
                AddCity(city);
            }
        }

        public Road AddRoad(double distance, City from, City to)
        {
            Road road = new Road(distance);
            from.AddRoad(road, to);
            to.AddRoad(road, from);
            ro_Roads.Add(road);
            return road;
        }

        public Road AddRoad(double distance, string from, string to)
        {
            City from_city = null;
            if (!ro_Cities.TryGetValue(from, out from_city))
            {
                from_city = AddCity(from);
            }

            City to_city = null;
            if (!ro_Cities.TryGetValue(to, out to_city))
            {
                to_city = AddCity(to);
            }
            return AddRoad(distance, from_city, to_city);
        }

        public Road AddRoad(Point from, Point to)
        {
            double distance = Pythagoras(from, to);
            City from_city = null;
            if (!ro_Cities.TryGetValue(from.ToString(), out from_city))
            {
                from_city = AddCity(from.ToString());
            }

            City to_city = null;
            if (!ro_Cities.TryGetValue(to.ToString(), out to_city))
            {
                to_city = AddCity(to.ToString());
            }
            return AddRoad(distance, from_city, to_city);
        }

        public List<City> AddCities (IList<Point> lop) 
        {
            Point[] pArray = lop.ToArray();
            List<City> ls = new List<City>();
            foreach (Point p in pArray)
            {
                AddCity(p.ToString());
            }

            return null;
        }

        private static double Pythagoras(Point p1, Point p2)
        {
            return Math.Sqrt(Math.Pow((p1.X - p2.X), 2) + Math.Pow((p1.Y - p2.Y), 2));
        }

    }
}
